/*
 * Copyright 2005-2013 biz-united. All rights reserved.
 * Support: http://www.biz-united.com.cn
 * License: http://www.biz-united.com.cn/?page_id=5&act=3
 */
package net.biz.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.FlushModeType;
import javax.persistence.TypedQuery;

import net.biz.dao.AreaDao;
import net.biz.dao.NativeBaseDao;
import net.biz.entity.Area;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.home766.dto.AreaDto;

/**
 * Dao - 地区
 * 
 * @author BIZ Team
 * @version 3.0
 */
@Repository("areaDaoImpl")
public class AreaDaoImpl extends BaseDaoImpl<Area, Long> implements AreaDao {
	@Autowired
	private NativeBaseDao nativeBaseDao;
	public List<Area> findRoots(Integer count) {
		String jpql = "select area from Area area where area.parent is null order by area.order asc";
		TypedQuery<Area> query = entityManager.createQuery(jpql, Area.class).setFlushMode(FlushModeType.COMMIT);
		if (count != null) {
			query.setMaxResults(count);
		}
		return query.getResultList();
	}

	@Override
	public List<AreaDto> findChildren(Long areaId) {
		// TODO Auto-generated method stub
		String sql = " select area.id as id ,area.name as name,area.full_name as full_Name from xx_area area"
				+ " where area.parent=:areaId";
		Map<String,Object> tempMap = new HashMap<String, Object>();
		tempMap.put("areaId", areaId);
		return nativeBaseDao.findList(sql, tempMap, AreaDto.class);
	}

}